package com.major.thread.synchronize;

public class BuyTickets implements Runnable {
    private int ticketNum = 10;

    boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            try {
                synchronized (this){
                    buy();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private  void buy() throws InterruptedException {
        if (ticketNum <= 0){
            flag = false;
            return;
        }

        Thread.sleep(1);

        System.out.println(Thread.currentThread().getName() + "---第" + ticketNum--);
    }

    public static void main(String[] args) {

        BuyTickets station = new BuyTickets();

        new Thread(station, "man1").start();
        new Thread(station, "man2").start();
        new Thread(station, "man3").start();


    }
}

